<?php
header('Content-Type: text/plain');
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/ecomutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
session_name(SESSION);
session_start();
$myPdo = DbUtils::createPdoInst();
$action = $_POST['action'];
try {
  $myPdo->beginTransaction();
  switch ($action) {
    case 'save':
      save($myPdo);
      break;
    case 'save_attr':
      save_attr($myPdo);
      break;
    case 'del_attr':
      del_attr($myPdo);
      break;
    case 'save_attr_opt':
      save_attr_opt($myPdo);
      break;
    case 'del_attr_opt':
      del_attr_opt($myPdo);
      break;
    case 'set_def':
      set_def($myPdo);
      break;
    case 'del_img':
      del_img($myPdo);
      break;
    default:
      break;
  }
  $myPdo->commit();
} catch (PDOException $e) {
  echo $e->getMessage();
}

function save($myPdo) {
  if (!isset($_SESSION['admin' . USER])) {
    return;
  }
  $q = json_decode(MiscUtils::getParam('q', '[]'));
  if (isset($q->id)) {
    DbUtils::update($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_product', $q);
  } else {
    $q->date = date('Y-m-d', time());
    DbUtils::add($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_product', $q);
  }
  //Update stocks & price
  foreach ($q->_objects as &$obj) {
    DbUtils::update($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_object', $obj);
  }
  
  //Update member price
  foreach ($q->_member_prices as &$mp) {
    if (isset($mp->id)) {
      DbUtils::update($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_member_price', $mp);
    } else {
      DbUtils::add($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_member_price', $mp);
    }
  }
  
  $cond_vals = new stdClass(); 
  $cond_vals->c = 't.product_track_id = :v1';
  $cond_vals->v = array(':v1' => $q->track_id);
  $opt = new stdClass();
  $opt->select_expr = 'SUM(t.id) AS sum';
  $total_attrs = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_attr', $cond_vals,  NULL, NULL, NULL, NULL, $opt)->d[0]->sum;
  if ($total_attrs > 0) {
    $cond_vals = new stdClass(); 
    $cond_vals->c = 't.product_track_id = :v1';
    $cond_vals->v = array(':v1' => $q->track_id);
    $objects = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_object', $cond_vals,  NULL, NULL, NULL, NULL, NULL)->d;
    EComUtils::update_alert($myPdo, $q, $objects);
  } else {
    EComUtils::update_alert($myPdo, $q, NULL);
  }
  
  echo json_encode(array());
}

function save_attr($myPdo) {
  if (!isset($_SESSION['admin' . USER])) {
    return;
  }
  $q = json_decode(MiscUtils::getParam('q', '[]'));
  if (isset($q->id)) {
    DbUtils::update($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_attr', $q);
  } else {
    DbUtils::add($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_attr', $q);
    $q->id = $myPdo->lastInsertId();
    //Attr must have ONE Opt minimally
    $opt = new stdClass();
    $opt->product_track_id = $q->product_track_id;
    $opt->product_attr_id = $q->id;
    $opt->name = 'Option';
    $opt->file = '';
    DbUtils::add($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_attr_opt', $opt);
    $opt->id = $myPdo->lastInsertId();
    $q->_opts = array($opt);
  }
  //Sync and reload
  __process_object($myPdo, $q);
}

function del_attr($myPdo) {
  if (!isset($_SESSION['admin' . USER])) {
    return;
  }
  $q = json_decode(MiscUtils::getParam('q', '[]'));
  DbUtils::delete($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_attr', $q);
  
  $cond_vals = new stdClass();
  $cond_vals->c = 'product_attr_id = :v1';
  $cond_vals->v = array(':v1' => $q->id);
  DbUtils::deleteByCond($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_attr_opt', $cond_vals);
  //Sync and reload
  __process_object($myPdo, $q);
}

function save_attr_opt($myPdo) {
  if (!isset($_SESSION['admin' . USER])) {
    return;
  }
  $q = json_decode(MiscUtils::getParam('q', '[]'));
  if (isset($q->id)) {
    DbUtils::update($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_attr_opt', $q);
  } else {
    DbUtils::add($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_attr_opt', $q);
    $q->id = $myPdo->lastInsertId();
  }
  //Sync and reload
  __process_object($myPdo, $q);
}

function del_attr_opt($myPdo) {
  if (!isset($_SESSION['admin' . USER])) {
    return;
  }
  $q = json_decode(MiscUtils::getParam('q', '[]'));
  DbUtils::delete($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_attr_opt', $q);
  //Sync and reload
  __process_object($myPdo, $q);
}

function set_def($myPdo) {
  if (!isset($_SESSION['admin' . USER])) {
    return;
  }
  $q = json_decode(MiscUtils::getParam('q', '[]'));
  $myPdo->exec('UPDATE ' . TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_img SET is_default = 0 WHERE product_track_id = \'' . $q->product_track_id . '\'');
  DbUtils::update($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_img', $q);
  echo json_encode(array());
}

function del_img($myPdo) {
  if (!isset($_SESSION['admin' . USER])) {
    return;
  }
  $q = json_decode(MiscUtils::getParam('q', '[]'));
  DbUtils::delete($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_img', $q);
  $obj_path = iconv('UTF-8', 'GB2312', $q->file);
  if (file_exists($obj_path)) {
    @unlink($obj_path);
  }
  $cond_vals = new stdClass(); 
  $cond_vals->c = 't.product_track_id = :v1';
  $cond_vals->v = array(':v1' => $q->product_track_id);
  $tmp = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_img', $cond_vals,  't.id', 'ASC', NULL, NULL, NULL)->d;
  if ($q->is_default == 1 && count($tmp) > 0) {
    $tmp[0]->is_default = 1;
    DbUtils::update($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_img', $tmp[0]);
  }
  echo json_encode($tmp);
}

function __process_object($myPdo, $q) {
  if (isset($q->_objects)) {
    foreach ($q->_objects as &$obj) {
      DbUtils::update($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_object', $obj);
    }
  }
  __sync_object($myPdo, $q->product_track_id);
  $cond_vals = new stdClass(); 
  $cond_vals->c = 't.product_track_id = :v1';
  $cond_vals->v = array(':v1' => $q->product_track_id);
  $q->_objects = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_object', $cond_vals,  't.attr_opt_code', 'ASC', NULL, NULL, NULL)->d;
  echo json_encode($q);
}

function __sync_object($myPdo, $product_track_id) {
  //All Attrs
  $cond_vals = new stdClass(); 
  $cond_vals->c = 't.product_track_id = :v1';
  $cond_vals->v = array(':v1' => $product_track_id);
  $attrs = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_attr', $cond_vals,  't.id', 'ASC', NULL, NULL, NULL)->d;
  foreach ($attrs as $obj) {
    $cond_vals = new stdClass(); 
    $cond_vals->c = 't.product_attr_id = :v1';
    $cond_vals->v = array(':v1' => $obj->id);
    $obj->_opts = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_attr_opt', $cond_vals,  't.id', 'ASC', NULL, NULL, NULL)->d;
  }
  
  $rslt = new stdClass();
  $rslt->arr = array();
  __get_object($attrs, 0, $rslt, '', '');
  
  //All objects
  $cond_vals = new stdClass(); 
  $cond_vals->c = 't.product_track_id = :v1';
  $cond_vals->v = array(':v1' => $product_track_id);
  $objects = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_object', $cond_vals,  NULL, NULL, NULL, NULL, NULL)->d;
  
  //Add and update
  foreach ($rslt->arr as $obj) {
    $exist = 0;
    foreach ($objects as $obj2) {
      if ($obj->attr_opt_id == $obj2->attr_opt_id) {
        $obj2->attr_opt_code = $obj->attr_opt_code;
        DbUtils::update($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_object', $obj2);
        $exist = 1;
        break;
      }
    }
    if ($exist == 0 && $obj->attr_opt_id != '') {
      $obj->product_track_id = $product_track_id;
      $obj->stock = 0;
      $obj->stock_alert = 0;
      $obj->diff_price = 0;
      DbUtils::add($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_object', $obj);
    }
  }
  
  //Delete
  foreach ($objects as $obj) {
    $exist = 0;
    foreach ($rslt->arr as $obj2) {
      if ($obj->attr_opt_id == $obj2->attr_opt_id) {
        $exist = 1;
        break;
      }
    }
    if ($exist == 0) {
      DbUtils::delete($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_object', $obj);
    }
  }
  
  //Update alert
  $cond_vals = new stdClass(); 
  $cond_vals->c = 't.product_track_id = :v1';
  $cond_vals->v = array(':v1' => $product_track_id);
  $opt = new stdClass();
  $opt->select_expr = 'SUM(t.id) AS sum';
  $total_attrs = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_attr', $cond_vals,  NULL, NULL, NULL, NULL, $opt)->d[0]->sum;
  if ($total_attrs > 0) {
    $cond_vals = new stdClass(); 
    $cond_vals->c = 't.product_track_id = :v1';
    $cond_vals->v = array(':v1' => $product_track_id);
    $objects = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_object', $cond_vals,  NULL, NULL, NULL, NULL, NULL)->d;
    EComUtils::update_alert($myPdo, $q, $objects);
  } else {
    EComUtils::update_alert($myPdo, $q, NULL);
  }
}

function __get_object($data_arr, $deepth, $rslt, $id_str, $code_str) {
  if (count($data_arr) <= $deepth) {
    $my_obj = new stdClass();
    $my_obj->attr_opt_id = $id_str;
    $my_obj->attr_opt_code = $code_str;
    $rslt->arr[] = $my_obj;
    return;
  }
  if ($deepth > 0) {
    $id_str .= '-';
    $code_str .= '+';
  }
  foreach ($data_arr[$deepth]->_opts as $obj) {
    __get_object($data_arr, $deepth + 1, $rslt, $id_str . $obj->id, $code_str . $obj->name);
  }
}
?>